package graph_viewer.drawer;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.RenderingHints;

import graph_viewer.structure.*;

public class Edge_Drawer implements Component_Drawer{
	//============================================================================
	// constructor
	//============================================================================
	public Edge_Drawer(){	
	}


	//============================================================================
	// implemented methods
	//============================================================================
	public void draw(Visual_Component component, Graphics g, int k){
		Edge edge = (Edge)component;

		Graphics2D g2d = (Graphics2D)g;
		draw_edge( edge, g2d );
	}

	
	//============================================================================
	// private methods
	//============================================================================
	private void draw_edge( Edge edge, Graphics2D g ){
		Graphics2D working_g;

		if ( is_important_edge( edge ) ){
			g	= find_highlighted_graphics( g );
		}else{
			g	= find_less_important_graphics( g );
		}

		g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );		
		
		int[] connect_points	= edge.find_start_end_location();
	
		Node_Position 
			start_node_position	= edge.get_start_node().get_position(),
			end_node_position	= edge.get_end_node().get_position();
		
		int[]
			start_node_center	= start_node_position.find_center(),
			end_node_center	= end_node_position.find_center();
		
		String type	= edge.get_type();

		if ( edge.is_default_type() ){
			g.drawLine(start_node_center[0], start_node_center[1], end_node_center[0], end_node_center[1]);
		}else{
			if ( type.equals( Visual_Component.EDGE_TYPE_cLinks ) ){
				g.drawLine(start_node_center[0], start_node_center[1], end_node_center[0], end_node_center[1]);
			}
		}
		
		set_normal_graphics( g );
	}
	
	private Graphics2D find_highlighted_graphics( Graphics2D g ){
		g.setColor( HIGHLIGHTED_EDGE_COLOR );
		g.setStroke( new BasicStroke( 1.0f )  );	

		return g;
	}

	private Graphics2D find_less_important_graphics( Graphics2D g ){
		g.setColor( NORMAL_EDGE_COLOR );
		g.setStroke( new BasicStroke( 0.5f )  );	

		return g;
	}
	
	private void set_normal_graphics( Graphics2D g ){
		
		g.setColor( EDGE_COLOR );
		g.setStroke( new BasicStroke( 1.0f )  );	
	}

	private boolean is_important_edge( Edge an_edge ){
		return an_edge.get_is_important();
		/*
		// this part is for software architecture: an important edge is an internal edge.
		Node 
			start_node	= an_edge.get_start_node(),
			end_node	= an_edge.get_end_node();
		
		String
			start_node_parent_label	= start_node.get_parent().get_label(),
			end_node_parent_label	= end_node.get_parent().get_label();

		if ( start_node_parent_label.equals( end_node_parent_label ) ){
			return true;
		}else{
			return false;
		}*/
	}
	/*
	private void draw_edge( Edge edge, Graphics2D g ){
		g.setColor( find_edge_color( edge )  );
		g.setStroke( find_edge_stroke( edge )  );		

		g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );		
		
		int[] connect_points	= edge.find_start_end_location();
	
		Node_Position 
			start_node_position	= edge.get_start_node().get_position(),
			end_node_position	= edge.get_end_node().get_position();
		
		int[]
			start_node_center	= start_node_position.find_center(),
			end_node_center	= end_node_position.find_center();
		
		String type	= edge.get_type();

		if ( edge.is_default_type() ){
			g.drawLine(start_node_center[0], start_node_center[1], end_node_center[0], end_node_center[1]);
		}else{
			if ( type.equals( Visual_Component.EDGE_TYPE_cLinks ) ){
				g.drawLine(start_node_center[0], start_node_center[1], end_node_center[0], end_node_center[1]);
			}
		}

		g.setStroke(  new BasicStroke( 1.0f ) );
	}

	private Color find_edge_color( Edge edge  ){
		Node 
			start_node	= edge.get_start_node(),
			end_node	= edge.get_end_node();
		
		String
			start_node_parent_label	= start_node.get_parent().get_label(),
			end_node_parent_label	= end_node.get_parent().get_label();

		if ( start_node_parent_label.equals( end_node_parent_label ) ){
			return INTERNAL_EDGE_COLOR;
		}else{
			return EXTERNAL_EDGE_COLOR;
		}
	}

	private BasicStroke find_edge_stroke( Edge edge  ){
		Node 
			start_node	= edge.get_start_node(),
			end_node	= edge.get_end_node();
		
		String
			start_node_parent_label	= start_node.get_parent().get_label(),
			end_node_parent_label	= end_node.get_parent().get_label();

		if ( start_node_parent_label.equals( end_node_parent_label ) ){
			return new BasicStroke( 1.0f );	
		}else{
			return new BasicStroke( 0.5f );	
		}
	}*/
}
